package wbbs.service;

import java.util.ArrayList;
import java.util.List;

public class PaginatedList<T> extends ArrayList<T> {

    private static final long serialVersionUID = 3717987307340661571L;

    private int page = -1;
    private int pageSize = -1;
    private int itemCount = -1;

    public PaginatedList(int size) {
        super(size);
        setPageSize(size);
    }

    public void setPage(int pg) {
        this.page = pg;
        if (itemCount == -1 || pageSize == -1) {
            throw new IllegalStateException();
        }
        if (page == -1) page = Integer.MAX_VALUE;
        if (page < 1) page = 1;
        int pc = getPageCount();
        if (page > pc) page = pc;
    }

    public void setPageSize(int pageSize) {
        if (pageSize <= 0) throw new IllegalArgumentException("pageSize: " + pageSize);
        this.pageSize = pageSize;
    }

    public void setItemCount(int itemCount) {
        if (itemCount < 0) throw new IllegalArgumentException();
        this.itemCount = itemCount;
    }

    public int getItemCount() {
        return itemCount;
    }

    public int getPage() {
        return page;
    }

    public int getPageCount() {
        if (page == -1) {
            throw new IllegalStateException();
        }
        if (itemCount == 0) return 1;
        int pc = itemCount / pageSize;
        if (itemCount % pageSize == 0) {
            return pc;
        }
        return pc + 1;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getOffset() {
        return (getPage() - 1) * pageSize;
    }

    public List<PageLink> getPgs(int size) {
        int pc = getPageCount();
        int pmin = Math.max(1, page - size);
        int pmax = pmin + size * 2;
        while (pmax > pc) {
            pmax--;
            if (pmin > 1) pmin--;
        }
        List<PageLink> pgs = new ArrayList<PageLink>();

        for (int i = 0; i < pmax - pmin + 1; i++) {
            PageLink pl = new PageLink();
            pl.page = pmin + i;
            pl.current = pl.page == page;
            pl.label = String.valueOf(pl.page);
            pgs.add(pl);
        }
        if (pgs.get(0).page > 1) {
            if (pgs.get(0).page > 2) {
                PageLink pl = new PageLink();
                pl.page = pgs.get(0).page - 1;
                pl.label = "...";
                pgs.add(0, pl);
            }
            PageLink pl = new PageLink();
            pl.page = 1;
            pl.label = "1";
            pl.current = pl.page == page;
            pgs.add(0, pl);
        }
        if (pgs.get(pgs.size() - 1).page < pc) {
            if (pgs.get(pgs.size() - 1).page < pc - 1) {
                PageLink pl = new PageLink();
                pl.page = pgs.get(pgs.size() - 1).page + 1;
                pl.label = "...";
                pgs.add(pl);
            }
            PageLink pl = new PageLink();
            pl.page = pc;
            pl.label = String.valueOf(pc);
            pl.current = pl.page == page;
            pgs.add(pl);
        }
        return pgs;
    }

    public List<PageLink> getPgs() {
        return getPgs(2);
    }
}

